﻿/* ************************************************************
 * Namespace: JumpFleetGameLib.xMap.SectorMap
 * Class:  BackgroundLayer
 * Author:  Roy Penrod
 * 
 * Class to organize the background layers in a sector map.  
 * Each layer contains a collection of BackgroundTile objects.
 *  
 * ************************************************************ */

using System;
using Microsoft.Xna.Framework;

namespace JumpFleetGameLib.xMap.SectorMap
{
    public class BackgroundLayer
    {
        #region MEMBERS

        // tag to identify the layer
        private string _tag;

        // depth the tiles in this layer will be drawn at
        private float _depth;

        // holds the Tile objects
        // [column, row]
        public BackgroundTile[,] Tiles;

        // flag for whether the layer is visible or not
        private bool _visible;

        // stores the width and height of the tiles
        private int _tileWidth;
        private int _tileHeight;

        #endregion


        #region PROPERTIES

        /// <summary>
        /// Tag to identify the background layer.  Type: string
        /// </summary>
        public string Tag
        {
            get { return _tag; }

            set { _tag = value; }
        }       

        /// <summary>
        /// Depth the tiles in this layer will be drawn at.  Sets the Layer property for each BackgroundTile.  Type: float
        /// </summary>
        public float Depth
        {
            get { return _depth; }
            set 
            {
                _depth = value;
                SetBackgroundTileDepth();
            }
        }
        
        /// <summary>
        /// Flag for whether the background layer is visible.  Sets the Visible property for each BackgroundTile.  Type: bool
        /// </summary>
        public bool Visible
        {
            get { return _visible; }
            set 
            {
                _visible = value;
                SetBackgroundTileVisible();            
            }
        }

        /// <summary>
        /// Returns the width of the background tiles.
        /// </summary>
        public int TileWidth
        {
            get { return _tileWidth; }            
        }

        /// <summary>
        /// Returns the height of the background tiles.
        /// </summary>
        public int TileHeight
        {
            get { return _tileHeight; }
        }

        #endregion


        #region CONSTRUCTORS
                
        /// <summary>
        /// Constructor for the BackgroundLayer class.  The Visible property defaults to false.
        /// </summary>
        /// <param name="tag">Tag to identify the background layer.  Type: string</param>
        /// <param name="numberOfColumns">The number of columns in this background layer.  Type: int</param>
        /// <param name="numberOfRows">The number of rows in this background layer.  Type: int</param>
        /// <param name="layerDepth">Depth the background tiles in this background layer will be drawn at.  Sets the Layer property for each BackgroundTile.  Type: float</param>
        /// <param name="tileWidth">The width of the background tiles. Type: int</param>
        /// <param name="tileHeight">The height of the background tiles.  Type: int</param>
        public BackgroundLayer(string tag, int numberOfColumns, int numberOfRows, float layerDepth, int tileWidth, int tileHeight)
        {
            // assigns the parameters
            _tag = tag;
            _depth = layerDepth;
            _tileWidth = tileWidth;
            _tileHeight = tileHeight;

            // creates the multi-dimensional array of Tile objects
            Tiles = new BackgroundTile[numberOfColumns, numberOfRows];

            // works through the grid, top to bottom, left to right
            for (int row = 0; row < numberOfRows; row++)
                for (int column = 0; column < numberOfColumns; column++)
                {
                    // figures out the Tile's position based on it's position in the grid
                    Vector2 tilePosition = new Vector2(column * tileWidth, row * tileHeight);

                    // adds a blank background tile to this position in the grid
                    Tiles[column, row] = new BlankBackgroundTile(tileWidth, tileHeight, layerDepth);
                    Tiles[column, row].Position = tilePosition;
                }            
        }

        #endregion


        #region METHODS
        
        /// <summary>
        /// Sets the Layer property of each BackgroundTile to the Depth property of this Layer object.
        /// </summary>
        private void SetBackgroundTileDepth()
        {
            // go through the Tiles array (top to bottom, left to right)
            // and set the Layer property of each BackgroundTile to the Depth
            // property of this Layer object
            for (int row = 0; row < Tiles.GetLength(1); row++)
                for (int column = 0; column < Tiles.GetLength(0); column++)
                    Tiles[column, row].Layer = Depth;           
        }

        /// <summary>
        /// Sets the Visible property of each BackgroundTile to the Visible property of this Layer object.
        /// </summary>
        private void SetBackgroundTileVisible()
        {
            // go through the Tiles array (top to bottom, left to right)
            // and set the Visible property of each BackgroundTile to the Visible
            // property of this Layer object
            for (int row = 0; row < Tiles.GetLength(1); row++)
                for (int column = 0; column < Tiles.GetLength(0); column++)
                    Tiles[column, row].Visible = Visible;
        }

        #endregion

    } // <-- end class
} // <-- end namespace
